<include file="header" />
<body>
<script type="text/javascript">
  var flag = 1;//优惠券全局判断 (用于弹框判断) 
</script>

<div class="wraper">
    <div class="head clearfix">
        <a href="javascript:history.back();" class="iconfont icon-zuo pull-left"></a>
        <span class="head-name pull-left">充值金额</span>
        <a href="{BEESCRM::U('/Charger/Chargingprox/index',array('accountId'=>$accountId))}" class="iconfont icon-shouye pull-left text-right"></a>
    </div>
    <div class="title padding tb-mode">
        <p class="tb-cell">请选择充值金额</p>
    </div>
    <div class="lists">
        <div class="time-box clearfix">
            <volist name="data['moneyList']" id="vo" key="k">
                <div class="time-list" data-amount="{BEESCRM:$vo.value}" data-money="{BEESCRM:$vo.money_id}" >
                    <p>{BEESCRM:$vo.value}元</p>
                </div>
            </volist>
        </div>
         
    </div>
    <!--优惠券-->
   {BEESCRM::W('Coupon',array('module_id'=>19,'account_id' => $accountId,'user_id' => $userId))}

    <notempty name="chargingTips">
        <div class="title padding tb-mode">
            <p class="tb-cell">说明</p>
        </div>
        <div class="lists text-12">
            {BEESCRM:$chargingTips}
        </div>

        <div class="padding tb-mode">
            <p class="text-center text-grey text-12">充值即视为同意以上条款</p>
        </div>
    </notempty>


    <div class="btn-box fixed_div">
        <a href="javascript:void(0);" class="btn btn-submit">立即充值</a>
    </div>
</div>

<input type="hidden" name="hour" id="hour" data-money="" data-moneyid="" data-hour=""/>

<include file="commonJS"/>


<script>
    seajs.use("$",function() {
        if($(document).height()>$(window).height()){
            $('.btn-box').addClass('fixed-btn');
            $('.wraper').css('paddingBottom',50);
        }else{
            $('.btn-box').removeClass('fixed-btn');
        }

        //初始化
        $('.time-box').find('.time-list:first-child').addClass('checked').siblings().removeClass('checked');
        $('#hour').data('money',$('.time-list:first-child').data('amount'));
        $('#hour').data('moneyid',$('.time-list:first-child').data('money'));
        $('#old_money').val($('.time-list:first-child').data('amount'));//优惠券插件
         coupon_change_money();
        /**
         * 选择充值时间
         */
        $('.time-box').on('click','.time-list',function(){
            $(this).addClass('checked').siblings().removeClass('checked');
            $('#hour').data('money',$(this).data('amount'));
            $('#hour').data('moneyid',$(this).data('money'));
            $('#old_money').val($(this).data('amount'));//优惠券插件
            coupon_change_money();
        })

        
        /**
         * 充值
         */
        $(document).on('click','.btn-submit',function(){

           /* var amount = Number($('.time-box').find('.checked').data('amount'));
            var money_id = $('.time-box').find('.checked').data('money')*/
            var amount = $('#hour').data('money');
            var money_id = $('#hour').data('moneyid');
            var record_id = $('#mycoupon').val();//优惠券id
            
            //var ratio_num = Number($('#ratio_num').val());
            var amountReg = /^[0-9]*[1-9][0-9]*$/;
            if(amount == 0){
                fm.alert('充值金额不能为空');return ;
            }else if(!amountReg.test(amount)){
                fm.alert('充值金额只能为正整数');return ;
            }

            $.ajax({
                type:'post',
                dataType:'json',
                data:{'goods_amount':amount , 'money_id':money_id,'record_id':record_id},
                url:"{BEESCRM::U('/Charger/RechargingProx/createOrder',array('accountId'=>$accountId,'chargingId'=>$chargingId))}",
                beforeSend: function(){
                    $('.load').show();
                },
                success:function(data){
                    console.log(data); 
                    $('.load').hide();
                    if(data.status == 'F'){
                        fm.alert(data.message);return ;
                    }else{
                        var id = data.data.order_id;
                        var wxpay = $.parseJSON(data.wxpay);
                        pay(wxpay, id);
                    }
                },
                error:function(data){
                    $('.load').hide();
                    fm.alert(data.message);
                    return false;
                }
            });
        });

        //数据处理错误
        function show_error(data){
            fm.alert(data.message);
            /* if(data.url){
             setTimeout('location.href="'+data.url+'"',2000);
             } */
        }

        //微支付
        function pay(wxpay, id){
            if (typeof WeixinJSBridge == "undefined"){
                if( document.addEventListener ){
                    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                }else if (document.attachEvent){
                    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                }
            }else{
                WeixinJSBridge.invoke(
                        'getBrandWCPayRequest',
                        wxpay,
                        function(res){
                            WeixinJSBridge.log(res.err_msg);
       
                            if(res.err_msg == "get_brand_wcpay_request:ok"){
                                //paySuccess(id);
                                var chargingId = "{BEESCRM:$chargingId}";
                                var terminalId = "{BEESCRM:$terminalId}";
                                if(chargingId && $terminalId){
                                    var hrefurl = "{BEESCRM::U('Charger/chargingprox/charge',array('accountId'=>$accountId,'chargingId'=>$chargingId,'terminalId'=>$terminalId))}";
                                }else{
                                    var hrefurl = "{BEESCRM::U('Charger/RechargingProx/chargingMoneyRecord',array('accountId'=>$accountId))}";
                                }
                                
                                setTimeout(function(){
                                    location.href=hrefurl;
                                }, 1000)
                            }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                                fm.alert('付款已取消');
                                return false;
                            }else{
//							$.alert('error','调用微支付失败');
                                fm.alert(res.err_msg);
                                return false;
                            }
                        }
                );
            }
        }

       /* function paySuccess(order_id){
        
            url = "{BEESCRM::U('Charger/RechargingProx/paysuccess',array('accountId'=>$accountId,'chargingId'=>$chargingId))}/order_id/"+order_id;

            $.ajax({
                url:url,
                dataType:"json",
                beforeSend:function(){
                    fm.showLoging('正在完成..')
                },
                success:function(data){
                    fm.closeLayer();
                    redirect(data);
                },
                error:function(data){
                    fm.alert(data.msg)
                }
            })
        }

        function redirect(data){
            location.href = data.url;
        }*/
    });
</script>

</body>
</html>